<?php

namespace App\Console\Commands\Cruise;

use App\Models\Cruise\CruiseCheckDetailModel;
use App\Models\Cruise\CruiseCheckSelfTaskModel;
use App\Models\Cruise\CruiseCheckTaskModel;
use App\Models\Cruise\CruiseTaskOrderMappingModel;
use App\Service\Cruise\Check\CruiseCheckTaskService;
use Illuminate\Console\Command;

class InitSelfTableType extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'cruise:init_self_table_type';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '初始化self相关表的type字段';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        CruiseCheckSelfTaskModel::query()
                                ->with('cruiseTemplateRel')
                                ->where('templateType', 0)
                                ->get()
                                ->each(function ($item) {
                                    if (!$item->cruiseTemplateRel) {
                                        return;
                                    }

                                    $item->templateType = $item->cruiseTemplateRel->type;
                                    $item->save();

                                    $this->info("CruiseCheckSelfTaskModel " . $item->id . ' ' . $item->templateType);
                                });

        \App\Models\Cruise\CruiseCheckSelfDetailModel::query()
                                                     ->with('cruiseOrderRel')
                                                     ->where('templateType', 0)
                                                     ->get()
                                                     ->each(function ($item) {

                                                         if (!$item->cruiseOrderRel) {
                                                             return;
                                                         }

                                                         $item->templateType = $item->cruiseOrderRel->type;
                                                         $item->save();

                                                         $this->info("CruiseCheckSelfDetailModel " . $item->id . ' ' . $item->templateType);
                                                     });
    }
}
